<template>
  <h1>个人中心</h1>
  <div v-for="(weibo,i) in  weiboList" style="width: 500px;margin: 0 auto;height: 40px " >
    <h3 style="float: left;margin: 0;line-height: 32px;color: darkgreen" >{{weibo.content}}</h3>
    <el-button type="danger" style="float: right" @click="del(weibo.id,i)">删除</el-button>
  </div>

</template>

<script setup>
import {onMounted, ref} from "vue";
import axios from "axios";
import {ElMessage} from "element-plus";
import router from "@/router";

//从localStroage中获取普通字符串,并转换为JSON字符串
let user=localStorage.user?JSON.parse(localStorage.user):null;

const weiboList=ref([]);
onMounted(()=>{
  console.log(user.id);
  axios.get("http://localhost:8080/v1/weibo/select?id="+user.id)
        .then((res)=>{
          if (res.data.code==2002) {
            weiboList.value = res.data.data;
            if (weiboList.value.length==0){
              ElMessage.warning("快发布微博吧");
              router.push('/post');
            }
          }
        })
});

const del=(weiboId,index)=>{
  console.log('本土微博的id'+weiboId+'和前端数组索引'+index);
  if (confirm("确定删除吗?")){
    axios.delete('http://localhost:8080/v1/weibo/delete/'+weiboId)
          .then((res)=>{
            if (res.data.code==2002){
              ElMessage.success('删除成功');
              weiboList.value.splice(index,1);
              if (weiboList.value.length==0){
                ElMessage.warning('记得发微博');
                router.push('/post');
              }
            }
          })
  }
}
</script>

<style scoped>

</style>